package leetcode;

import leetcode.bean.ListNode;

import java.math.BigDecimal;

/**
 * 练习题.
 * 2.两数相加
 *
 * @author :yanlixiong
 * @date :2024-02-25 11:50:00
 */
public class Exercises3 {

    public static void main(String[] args) {
        //--------------示例1--------------
        ListNode l11 = new ListNode(2);
        l11.next = new ListNode(4);
        l11.next.next = new ListNode(3);

        ListNode l12 = new ListNode(5);
        l12.next = new ListNode(6);
        l12.next.next = new ListNode(4);
        println(l11, l12);

        //--------------示例2--------------
        ListNode l21 = new ListNode(0);

        ListNode l22 = new ListNode(0);
        println(l21, l22);

        //--------------示例3--------------
        ListNode l31 = new ListNode(9);
        l31.next = new ListNode(9);
        l31.next.next = new ListNode(9);
        l31.next.next.next = new ListNode(9);
        l31.next.next.next.next = new ListNode(9);
        l31.next.next.next.next.next = new ListNode(9);
        l31.next.next.next.next.next.next = new ListNode(9);

        ListNode l32 = new ListNode(9);
        l32.next = new ListNode(9);
        l32.next.next = new ListNode(9);
        l32.next.next.next = new ListNode(9);
        println(l31, l32);
    }

    public static void println(ListNode l1, ListNode l2) {
        String s1 = "[";
        ListNode current1 = l1;
        while (current1 != null) {
            s1 += current1.val + ","; // 输出当前节点的值
            current1 = current1.next; // 移动到下一个节点
        }
        s1 = s1.substring(0, s1.length() - 1);
        s1 +="]";

        String s2 = "[";
        ListNode current2 = l2;
        while (current2 != null) {
            s2 += current2.val + ","; // 输出当前节点的值
            current2 = current2.next; // 移动到下一个节点
        }
        s2 = s2.substring(0, s2.length() - 1);
        s2 +="]";

        System.out.println("输入：l1=" + s1 + ",l1=" + s2);

        ListNode result = addTwoNumbers(l1,l2);
        String s3 = "[";
        ListNode current3 = result;
        while (current3 != null) {
            s3 += current3.val + ","; // 输出当前节点的值
            current3 = current3.next; // 移动到下一个节点
        }
        s3 = s3.substring(0, s3.length() - 1);
        s3 +="]";

        System.out.println("输出：" + s3);
        System.out.println("");
    }


    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        /*
         *
         * 1、先将链表的节点值用字符串拼接起来，然后利用StringBuilder的reverse()函数，进行字符串反转
         * 2、将numStr1和numStr2转成整数进行相加操作
         * 3、将sumBig转成字符串并进行反转
         * 4、将result转成链表并返回
         *
         */
        StringBuilder numStr1 = new StringBuilder();
        while (l1 != null) {
            numStr1.append(l1.val);
            l1 = l1.next;
        }
        numStr1.reverse();
        StringBuilder numStr2 = new StringBuilder();
        while (l2 != null) {
            numStr2.append(l2.val);
            l2 = l2.next;
        }
        numStr2.reverse();
        BigDecimal sumBig = new BigDecimal(numStr1.toString()).add(new BigDecimal(numStr2.toString()));
        StringBuilder result = new StringBuilder(sumBig.toString());
        result.reverse();
        String[] split = result.toString().split("");

        ListNode nodeSta = new ListNode(Integer.parseInt(split[0]));
        ListNode nextNode;
        nextNode = nodeSta;
        for (int i = 1; i < split.length; i++) {
            ListNode node = new ListNode(Integer.parseInt(split[i]));
            nextNode.next = node;
            nextNode = nextNode.next;
        }
        nextNode = nodeSta;
        return nextNode;
    }
}
